<?php
include_once(APPPATH."libraries/MY_Controller.php");

class AdminController extends My_Controller{ 
	
	var $footer_page 	= array();
	var $header_page	= array();
	var $per_page 		= 20;
	var $uri_segment 	= 4;
	var $links			= array();
	var $links_table	= array();
	var $links_table_item = array();
	var $cat_search 	= array();
	var $own_link 		= ""; 
	var $data_table;
	var $data_form;
	var $status_formulir = array();
	
	protected $uriNotLoginAllowed = array( 
		'/auth'
	);
	
	var $arr_actions=array(); 
	function __construct(){  

		parent::__construct(); 		  
		
		$this->jCfg['theme'] = 'admin/swellow';
		$this->_releaseSession();  
		$this->_set_sess_action();
		$this->_set_own_link();
		$this->load->helper('directory');
		if(!in_array($this->uri->uri_string,$this->uriNotLoginAllowed) && $this->jCfg['is_login'] != 1){
			redirect(modUri().'auth');
		}
		
		if( ($this->uri->uri_string == '/auth' OR $this->uri->uri_string == '/cms') && $this->jCfg['is_login'] == 1){
			redirect(base_url().'meme/me');
		} 
		
		if($this->_getClass()!= 'me' && $this->_getClass()!= 'uploadttd' && $this->_getClass()!= 'auth' && $this->_getClass()!= 'profile'){
			$this->_set_array_actions();
			if(in_array($this->_getMethod(),$this->arr_actions)){
				if(!_ac($this->_getMethod())){
					die('Access Denied, please contact your web Administrator..');
				}
			}
		}
		$this->setMenu(_get_menu($this->_set_sess_menu()));
		
		$this->status_formulir = array(
			"E"	=> "ENTRY",
			"P"	=> "PENDING",
			"U"	=> "UPLOAD",
			"P2"=> "PROSES ASURANSI",
			"RA"=> "REJECT ASURANSI",
			"V"	=> "VALID",
			"L" => "CANCEL",
			"W" => "APPROVE",
			"R"	=> "REPORT",
			"A"	=> "APPROVE",
			"AR"=> "ARSIP" 
		);
	}
	
	function _set_array_actions(){
		$q=$this->db->query("select action from macs_acl_actions")->result();
		foreach($q as $r){
			$this->arr_actions[]=$r->action;
		}
	}
	
	function _set_sess_action(){
		$sql = $this->db->query("
			select 
			maa.action,maa.action_image,mac.access_name,maa.action_type,
			mac.group_controller,mac.controller_name,maa.action_name
			from macs_user mu
				left join macs_acl_user_group maug on mu.user_id = maug.user_id
				left join macs_acl_group mag on mag.id = maug.group_id
				left join macs_acl_group_accesses magc on mag.id = magc.group_id
				left join macs_acl_actions maa on maa.id = magc.action_id
				left join macs_acl_accesses mac on mac.id = magc.access_id
			where 
			mag.group_status = 1
			AND mu.app_id = '".$this->jCfg['app_id']."'
			AND mu.user_id = '".$this->jCfg['user']['id']."'
			AND mac.controller_name = '".strtolower($this->_getClass())."'   
		")->result();
		$acc=array();
		if(count($sql) > 0){
			foreach($sql as $r){
				$acc[$r->action] = $r;
			}
		}
		$this->setAccess($acc);
	}
	
	function _set_sess_menu(){
		$menu = $this->db->query("
			select 
				mac.*
			from macs_user mu
				left join macs_acl_user_group maug on mu.user_id = maug.user_id
				left join macs_acl_group mag on mag.id = maug.group_id
				left join macs_acl_group_accesses magc on mag.id = magc.group_id
				left join macs_acl_actions maa on maa.id = magc.action_id
				left join macs_acl_accesses mac on mac.id = magc.access_id
			where 
			mag.group_status = 1
			AND mu.app_id = '".$this->jCfg['app_id']."'
			AND mu.user_id = '".$this->jCfg['user']['id']."'
			AND maa.action = 'index'
			GROUP BY mac.id
			ORDER BY by_order,access_name ASC
		")->result_array();
		return $menu;
	}
	
	function _set_action($v=array("bug","add","index"),$type='MAIN'){
		if(count($v)>0){
			$acc = $this->jCfg['access'];
			foreach($v as $r){
				if(isset($acc[$r])){
					$arr_action = array(
						'title'	=> _l($r),
						'action'=> $r,
						'type'	=> $acc[$r]->action_type,
						'link'	=> modUri().$acc[$r]->group_controller."/".$acc[$r]->controller_name."/".$r,
						'image'	=> $acc[$r]->action_image
					);
					if($type=='MAIN'){
						$this->links[] = $arr_action;
					}elseif($type=='TABLE'){
						$this->links_table[] = $arr_action;
					}elseif($type=='ITEM'){
						$this->links_table_item[] = $arr_action;
					}else{
						$this->links[] = $arr_action;
					}
				}
			}
		}
	}
	
	function _set_own_link(){
		$tlink=strtolower($this->_getClass());
		if(	isset($this->jCfg['access']['index']) && count($this->jCfg['access']['index']) > 0){
			$tlink = $this->jCfg['access']['index']->group_controller."/".strtolower($this->_getClass())."/";
		}
		$this->own_link = modUri().$tlink;
	}
	
	function _v($file,$data=array(),$header=TRUE){
		$this->header_page['val']		= $this->data_form;
		$this->header_page['message']	= $this->message;
		if($header==TRUE){
			$this->header_page['links'] 		= $this->links;
			$this->header_page['links_table']	= $this->links_table;
			$this->header_page['links_table_item']	= $this->links_table_item;
			$this->header_page['own_links']	= $this->own_link;
			$this->load->view($this->jCfg['theme'].'/header',$this->header_page);
			$this->load->view($this->jCfg['theme'].'/'.$file,$data);
			$this->load->view($this->jCfg['theme'].'/footer',$this->footer_page);
		}else{
			$data['val'] = $this->header_page['val'];
			$this->load->view($this->jCfg['theme'].'/'.$file,$data);
		}
	}
	
	function _data($m=array()){
		
		$this->load->library('pagination');
		$config['per_page'] = $this->per_page;
		$data = $this->data_table;
		$config['base_url'] = $m['base_url'];
		$config['next_link'] ="&raquo;";
		$config['prev_link'] ="&laquo;";
		$config['total_rows'] = $data['count']->found_rows;		
		$config['uri_segment'] = $this->uri_segment;
		$config['full_tag_open'] = '<div class="paging">';
		$config['full_tag_close'] = '</div>';	
		$this->pagination->initialize($config);
		
		return array(
			'data'			=>	$data['data'],			
			'cRec'			=>  $data['count']->found_rows,
			'no'			=>  $this->uri->segment($this->uri_segment)==''?0:$this->uri->segment($this->uri_segment),
			'cPage'			=>  ceil($data['count']->found_rows/$this->per_page),
			'paging'		=> 	$this->pagination->create_links()
		);		
	}	
	
	function _save_master($data=array(),$par=array(),$vid=0){
		$id 	= 0;
		$act	= FALSE;
		$o = $this->DATA->_cek($par);
		if( $o == 0 ){
			if($this->db->field_exists('user_add',$this->DATA->table)){			
				$data['user_add'] = $this->jCfg['user']['id'];
			}
			if($this->db->field_exists('time_add',$this->DATA->table)){	
				$data['time_add'] = date("Y-m-d H:i:s");
			}
			if($this->db->field_exists('app_id',$this->DATA->table)){	
				$data['app_id'] = $this->jCfg['app_id'];
			}
			$act = $this->DATA->_add($data);
			$id = $this->db->insert_id();
		}else{
			if($this->db->field_exists('user_update',$this->DATA->table)){			
				$data['user_update'] = $this->jCfg['user']['id'];
			}
			if($this->db->field_exists('time_update',$this->DATA->table)){	
				$data['time_update'] = date("Y-m-d H:i:s");
			}
			$act = $this->DATA->_update($par,$data);
			$id = $vid;
		}
		return array(
			'id'	=> $id,
			'msg'	=> ($act)?_l('success_exec'):_l('fail_exec')
		);
	}
	
	function _set_title($t=''){
		return $this->header_page['title'] = $t;
	}
	
	function _set_cat_search($arr=array()){
		return $this->header_page['cat_search'] = $arr;
	}
	
	function _set_desc_title($t=''){
		return 	$this->header_page['desc_title'] = $t;
	}
	
	function _multiUpload($p=array()){
		$folder = date("Y-m-d");
		$path   = $this->upload_path;
		if(!is_dir($this->upload_path.$folder)){
			mkdir($path.$folder);
		}		
		$tpath = $path.$folder."/";	
		if(!is_dir($tpath.$p['id'])){
			mkdir($tpath.$p['id']);
		}		
		
		$newpath = $tpath.$p['id'];
		// config ..
		$config['upload_path'] 		= $newpath;
		$config['allowed_types'] 	= "pdf|doc|docx|xls|xlsx|png|gif|jpg";
		$config['max_size']			= 1024*10;
		
		$this->load->library('upload');
		$this->upload->initialize($config);	
		
		if ($this->upload->do_upload($p['name'])){	
		}else{}
		
		return $newpath;
	}	
	
}